package com.lanxin.jet.services.impl;

import java.util.List;
import java.util.Map;

import com.lanxin.jet.beans.ResourceBean;
import com.lanxin.jet.mappers.ResourceMapper;
import com.lanxin.jet.services.ResourceService;
import com.lanxin.jet.tools.JetCriteria;
import com.lanxin.jet.tools.JetDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public String insert(String bid, String type, String path, long size) {
        int sort = 1;
        Map<String, Integer> sortMap = resourceMapper.findNewSort(bid);
        if (sortMap != null) {
            sort += sortMap.get("sort");
        }
        ResourceBean resource = new ResourceBean();
        resource.setId(JetDate.uniqid());
        resource.setBid(bid);
        resource.setType(type);
        resource.setPath(path);
        resource.setSize(size);
        resource.setSort(sort);
        resource.setTime(JetDate.timeNow());
        resourceMapper.insert(resource);
        return resource.getId();
    }

    @Override
    public int count(String bid, String keyword) {
        JetCriteria criteria = new JetCriteria();
        criteria.add("bid", bid);
        criteria.addLike("name", keyword);
        return resourceMapper.count(criteria);
    }

    @Override
    public ResourceBean get(String id) {
        return resourceMapper.query(id);
    }

    @Override
    public List<ResourceBean> getList(int offset, int limit, String bid, String keyword) {
        JetCriteria criteria = new JetCriteria();
        criteria.add("bid", bid);
        criteria.addLike("name", keyword);
        criteria.setPage(offset, limit);
        criteria.orderBy("sort", JetCriteria.ORDER_DESC);
        return resourceMapper.queryList(criteria);
    }

    @Override
    public void update(ResourceBean resource) {
        resourceMapper.update(resource);
    }

    @Override
    public void delete(String id) {
        resourceMapper.delete(id);
    }

}